package com.sy.blog.config;

import com.sy.blog.handle.LoginInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 1、忘了添加@Configuration注解导致跨域不成功
 */
@Configuration
public class MyWebMVCConfig implements WebMvcConfigurer {

    @Autowired
    private LoginInterceptor loginInterceptor;

    /**
     * 配置跨域请求
     * @param registry
     */
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        // 所有请求，被允许访问http://localhost:8080
        registry.addMapping("/**")
//                .allowedOrigins("http://localhost:8080")
//                .allowedOrigins("https://www.syisme.com")
//                .allowedOrigins("https://blog.syisme.com")
//                .allowedOrigins("https://blog.syisme.com")
//                .allowedOrigins("http://47.96.110.182:8080")
//                .allowedOrigins("https://47.96.110.182:8888")
//                .allowedOrigins("https://47.96.110.182:8080")
//                .allowedOrigins("http://47.96.110.182:8888")
//                .allowedOrigins("http://47.96.110.182")
//                .allowedOrigins("https://47.96.110.182")
                .allowedMethods("GET", "POST");
    }

    /**
     * 文章访问这些无需用户登录
     * @param registry
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //拦截test接口，后续实际遇到需要拦截的接口时，在配置为真正的拦截接口
        registry.addInterceptor(loginInterceptor)         // 次数排除的需要加上/
        .addPathPatterns("/test")
        .addPathPatterns("/comments/create/change")
        .addPathPatterns("/articles/publish");
    }




}
